package com.weblog.article.controller;

import com.weblog.model.article.Comment;
import com.weblog.article.service.CommentService;
import constants.RedisKey;
import entity.PageResult;
import entity.Result;
import entity.ResultMessage;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * comment控制器层
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/comment")
public class CommentController {

	@Autowired
	private CommentService commentService;

	@Autowired
    private HttpServletRequest request;
	@Autowired
    private RedisTemplate jsonRedisTemplate;


	/**
	 * 多条件分页查询
	 * @param page
	 * @param size
	 * @param searchMap
	 * @return
	 */
	@PostMapping("search/{page}/{size}")
	public Result qureyByPage(@PathVariable int page, @PathVariable int size, @RequestBody Map<String,Object> searchMap){
		Page<Comment> commentPage=commentService.qureyByPage(page,size,searchMap);
		return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,new PageResult<Comment>(commentPage.getTotalElements(),commentPage.getContent()));
	}


	/**
	 * 根据id查询
	 * @return
	 */
	@GetMapping("/{id}")
	public Result queryById(@PathVariable ("id")String id){
        Comment comment = commentService.findById(id);
        if(comment==null){
            return new Result(false, StatusCode.ERROR, ResultMessage.COMMENT_NOT_FOUND);

        }
        return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,comment);
	}

	/**
	 * 根据父级id查询父级评论
	 * @return
	 */
	@GetMapping("/parent/{parentid}")
	public Result queryParentCommentByParentId(@PathVariable ("parentid")String parentid){
        Comment comment = commentService.queryParentCommentByParentId(parentid);
        if(comment==null){
            return new Result(false, StatusCode.ERROR, ResultMessage.PARENT_COMMENT_NOT_FOUND);
        }
        return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,comment);
	}


    /**
     * 根据id查询下一级评论
     * @param id
     * @return
     */
	@PostMapping("/childrens/{id}")
    public Result queryNextLevelComments(@PathVariable("id")String id){
      List<Comment> comments=commentService.queryNextLevelComments(id);
        return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,comments);
    }

    /**
     * 发表评论
     * @return
     */
    @PostMapping("/publish")
    public Result publishComment( @RequestBody Comment comment){
	   int flag= commentService.publishComment(comment);
        if(flag==2){
            return new Result(false, StatusCode.ACCESSERROR, ResultMessage.ACCESS_ERROR);
        }else if(flag==0){
            return new Result(false, StatusCode.ERROR, ResultMessage.LOGIN_USER_ABNORMAL);

        }
        return new Result(true, StatusCode.OK, ResultMessage.PUBLISH_SUCCESS);

    }



    /**
     * 回复评论
     * @param comment
     * @return
     */
	@PostMapping("/reply")
    public Result reply(@RequestBody Comment comment){

	    int flag=commentService.reply(comment);
	    if(flag==2){
	        return new Result(false, StatusCode.ACCESSERROR, ResultMessage.ACCESS_ERROR);
        }else if(flag==0){
            return new Result(false, StatusCode.ERROR, ResultMessage.LOGIN_USER_ABNORMAL);

        }
        return new Result(true, StatusCode.OK, ResultMessage.REPLY_SUCCESS);
    }

    /**
     * 评论点赞
     * @param id
     * @return
     */
    @PutMapping("/thumbup/{id}")
    public Result thumbup(@PathVariable("id")String id){
        //判断是否已经点过赞
        String userid= (String) request.getAttribute("id_A");
        String key=String.format(RedisKey.THUMB_UP_COMMENT,userid,id);
        if(jsonRedisTemplate.opsForValue().get(key)!=null){
            //点过赞
            return new Result(false, StatusCode.REPERROR, ResultMessage.REPEAT_THUMBUP);
        }
	    int flag=commentService.thumbup(id);
        if(flag==0){
            return new Result(false, StatusCode.ERROR,"评论好像不存在了哦");
        }
        jsonRedisTemplate.opsForValue().set(key,"1");
        return new Result(true, StatusCode.OK, ResultMessage.THUMBUP_SUCCESS);
    }

    /**
     * 禁用评论
     * @param id
     * @return
     */
    @PutMapping("/disabled/{id}")
    public Result disableComment(@PathVariable("id")String id){
       int flag= commentService.disableComment(id);
       if(flag==2){
           return new Result(false, StatusCode.ACCESSERROR, ResultMessage.ACCESS_ERROR);
       }
       return new Result(true, StatusCode.OK, ResultMessage.DISABLED_SUCCESS);
    }


    @DeleteMapping()
    public Result deleteComments(@RequestBody Map<String,String>map){
        commentService.deleteComment(map);
        return new Result(true,StatusCode.OK,ResultMessage.DELETE_SUCCESS);
    }
    /**
     * 根据文章id集合查询评论数量
     * @param aids
     * @return
     */
    @PostMapping("client/count")
    public Result countCommentByAids(@RequestBody List<String>aids){
      Long count=  commentService.countCommentByAids(aids);
      return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,count);
    }

    /**
     * 查询总记录数
     * @return
     */
    @GetMapping("/client/sum")
    public Result countTotal(){
        long count=commentService.count();
        return new Result(true, StatusCode.OK,"评论总记录数查询："+count, count);

    }


}
